package com.example.note.datahandler;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.axdac.mixbind.view.Bind;
import com.axdac.mixbind.view.MixBind;
import com.example.note.NoteActivity;
import com.example.note.R;
import com.example.note.cardlistview.CommonAdapter;
import com.example.note.dao.NoteDAO;
import com.example.note.dbtool.MyDBHelper;
import com.example.note.model.TNote;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ShowNoteHandler implements CommonAdapter.DataHandler<JSONObject> {
    private int viewid = 0x0017;
    public final static String type = "0017";

    @Bind(viewId = R.id.check_text)
    public TextView textView;

    @Bind(viewId = R.id.check_content)
    public TextView text;

    @Bind(viewId = R.id.view_container)
    public LinearLayout linearLayout;

    public NoteDAO taskNoteDAO;
    @Override
    public View getView(Context context, int viewType) {
        if (viewType != viewid) {
            return null;
        }
        taskNoteDAO = (NoteDAO) new MyDBHelper(context).getDAO(NoteDAO.class);
        return View.inflate(context, R.layout.view_note,null);
    }

    @Override
    public int getItemViewType(JSONObject item) {
        try {
            if (item.getString(CommonAdapter.TYPE_NAME).equals(type)) {
                return viewid;
            }
            return CommonAdapter.NOT_MATCH_TYPE;
        } catch (JSONException e) {
            e.printStackTrace();
            return CommonAdapter.NOT_MATCH_TYPE;
        }
    }

    @Override
    public boolean onBindViewHolder(int viewType, final CommonAdapter.MyViewHolder holder, JSONObject item) {
        if (viewType != viewid) {
            return false;
        }
        try {
            MixBind.OnlyBindView(holder.view,this);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        try {
            final TNote tTaskNote = (TNote) item.get("model");
            textView.setOnClickListener(null);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
            textView.setText(tTaskNote.title+"\n"+simpleDateFormat.format(new Date(Long.parseLong(tTaskNote.recordDate))));
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(holder.view.getContext(), NoteActivity.class);
                    intent.putExtra("noteID",tTaskNote.ID);
                    holder.view.getContext().startActivity(intent);
                }
            });

            text.setText(tTaskNote.content);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }
}
